﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using SqlCon;
using System.Data.SqlClient;

namespace yfxt
{
    public partial class gl_ypckd : Form
    {
        public gl_ypckd()
        {
            InitializeComponent();
        }

        string comStr;
        int howmanyrow; //DatagridView1 当前焦点所在行
        int tabSB = 0;    //在出库数量的列，防止按了Tab直接跳到下一行，而没有进行CellValueChanged的（计算金额和合计）
        int totalparameter = 0;    //dataGridView1.Rows[i].Cells[1].Value为null会报错，所以添加一个变量来避免报错((出库数量回车)当第一行数据显示在datagridView1上，值变为1)
        int tan_tishi;   //按确认按钮不弹提示框，变量 1为不弹，0为弹
        string ypmc;

        private void gl_ypckd_Load(object sender, EventArgs e)
        {
            tan_tishi = 0;
            textBox1.Text = ((gl_ypck)this.Owner).ckfs;                               //出库方式
            textBox2.Text = DateTime.Now.ToString("yyyy-MM-dd").Replace('-', '.');    //出库日期
            textBox3.Focus();

            #region No100004
            DataTable dt1 = new DataTable();            
            comStr = "select max(cKDH)+1 from YF_cK01";
            dt1.Load(SqlHelper.ExecuteReader(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, comStr, null));
            label6.Text = dt1.Rows[0][0].ToString();
            if (label6.Text=="")
            {
                label6.Text = "100001";
            }
            #endregion

            dataGridView2.Visible = false;   //隐藏dataGridView2
            dataGridView3.Visible = false;   //隐藏dataGridView3
            dataGridView1.Columns[1].ReadOnly = true;
            dataGridView1.Columns[2].ReadOnly = true;
            dataGridView1.Columns[3].ReadOnly = true;
            dataGridView1.Columns[4].ReadOnly = true;
            dataGridView1.Columns[6].ReadOnly = true;
            dataGridView1.Columns[7].ReadOnly = true;
            dataGridView1.Columns[8].ReadOnly = true;
            dataGridView1.Columns[9].ReadOnly = true;
            foreach (DataGridViewColumn c in dataGridView1.Columns)        //禁止对datagridView列进行排序
            {
                c.SortMode = DataGridViewColumnSortMode.NotSortable;
            }
            //方法2
            //for (int i = 0; i < dataGridView1.Columns.Count; i++)
            //{
            //    dataGridView1.Columns[i].SortMode= DataGridViewColumnSortMode.NotSortable
            //}
            dataGridView1.AllowUserToResizeRows = false;                     //禁止用鼠标调整datagridView单元格的大小。
            dataGridView1.AllowUserToResizeColumns = false;
            //方法2
            //dataGridView1.RowTemplate.Resizable = DataGridViewTriState.False;

            dataGridView3.Columns.Add("1", "药品批号");
            dataGridView3.Columns.Add("2", "产地");
            dataGridView3.Columns.Add("3", "库存数量");
            dataGridView3.Columns[0].Width = 85;
            dataGridView3.Columns[1].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            dataGridView3.Columns[2].Width = 85;
        }

        #region 备注textBox回车，跳到datagridView1
        private void textBox3_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (dataGridView1.Rows.Count == 0)
                {
                    dataGridView1.Rows.Add(1);
                    //dataGridView1.Focus();   不注释这行代码，datagridView1的第一行第一列不会获取焦点，我不明白
                    dataGridView1.CurrentCell = dataGridView1.Rows[0].Cells[0];
                    this.dataGridView1.BeginEdit(true);
                }
            }
        }
        #endregion

        private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Tab)
            {
                tabSB = 1;
            }
            else
            {
                tabSB = 0;
            }
        }

        private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            #region 输出出库数量回车，datagridView1增加一行
            //这段代码放在dataGridView1_CellValidated，导致datagridView2不能上下选择和接下来得操作不能进行，报错的原因

            if (tabSB != null)
            {
                for (int i = 0; i < dataGridView1.Rows.Count; i++)
                {
                    //if 加多一个条件 dataGridView1.Rows[i].Cells[5].Value != "0" 就没有报错，搞不清楚什么原理
                    if (dataGridView1.CurrentCell == dataGridView1.Rows[i].Cells[5] && dataGridView1.Rows[i].Cells[5].Value != null &&
                        dataGridView1.Rows[i].Cells[5].Value.ToString() != "0" && tabSB == 0)
                    {
                        if (float.Parse(dataGridView1.Rows[i].Cells[5].Value.ToString()) <= float.Parse(dataGridView1.Rows[i].Cells[9].Value.ToString()))
                        {
                            if (i == dataGridView1.Rows.Count - 1)
                            {
                                dataGridView1.Rows.Add(1);
                                dataGridView2.Top = 135 + 23 * (dataGridView1.Rows.Count - 1);      //datagridView2根据datagridView1的行数改变location
                                dataGridView3.Top = 135 + 23 * (dataGridView1.Rows.Count - 1);      //datagridView3根据datagridView1的行数改变location
                                dataGridView1.CurrentCell = dataGridView1.Rows[i + 1].Cells[0];
                                this.dataGridView1.BeginEdit(true);
                            }
                            else
                            {
                                dataGridView1.CurrentCell = dataGridView1.Rows[i].Cells[5];
                            }
                        }
                        else
                        {
                            dataGridView1.Rows[i].Cells[5].Value = null;
                            MessageBox.Show("库房没有那么多库存！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        }
                    }
                }
            }
            #endregion
        }

        private void dataGridView1_CellValidated(object sender, DataGridViewCellEventArgs e)
        {

            #region 输出药品名称回车
            if (tabSB != null)         //if(dataGridView1.Focus())这代码，导致焦点不能去到 备注textBox
            {
                for (int i = 0; i < dataGridView1.Rows.Count; i++)
                {
                    //输出药品拼音码或药品名称，弹出搜索窗口datagridView2
                    if (dataGridView1.CurrentCell == dataGridView1.Rows[i].Cells[0] && dataGridView1.CurrentRow.Cells[0].Value != null
                        && dataGridView1.CurrentRow.Cells[1].Value == null)
                    {
                        DataTable dt4 = new DataTable();
                        comStr = "select YPMC,ypgg,YFDW from yk_typk where PYDM like '" + dataGridView1.CurrentRow.Cells[0].Value.ToString() + "%'" +
                            " or ypmc like '%" + dataGridView1.CurrentRow.Cells[0].Value.ToString() + "%'";
                        dt4.Load(SqlHelper.ExecuteReader(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, comStr, null));

                        dataGridView2.DataSource = dt4;
                        dataGridView2.Columns[0].Width = 165;
                        dataGridView2.Columns[1].Width = 85;
                        dataGridView2.Columns[2].Width = 60;
                        if (dt4.Rows.Count != 0)
                        {
                            dataGridView2.Visible = true;
                            dataGridView2.Focus();
                            howmanyrow = i;
                        }
                    }
                    //计算金额。出库数量为null，自动填写0。获取合计标识变量。
                    else if (dataGridView1.CurrentCell == dataGridView1.Rows[i].Cells[5] && dataGridView1.CurrentRow.Cells[1].Value != null)
                    {
                        if (dataGridView1.CurrentRow.Cells[5].Value == null)
                        {
                            dataGridView1.CurrentRow.Cells[5].Value = "0";
                        }
                        dataGridView1.Rows[i].Cells[6].Value = ((float.Parse(dataGridView1.Rows[i].Cells[4].Value.ToString())) *
                            (float.Parse((dataGridView1.Rows[i].Cells[5].Value.ToString())))).ToString();
                        totalparameter = 1;
                    }
                }


                #region 输出出库数量，计算 合计
                if (totalparameter == 1)
                {
                    float float1 = 0.00f;
                    for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)     //datagridView1总会有一行空白行。
                    {
                        if (dataGridView1.Rows[i].Cells[1].Value.ToString() != null)
                        {
                            float1 += float.Parse((dataGridView1.Rows[i].Cells[6].Value.ToString()));
                        }
                    }
                    textBox6.Text = float1.ToString();
                }
                #endregion
            }
            #endregion
        }

        private void dataGridView2_KeyDown(object sender, KeyEventArgs e)
        {
            #region Esc关闭搜索药品窗口(DatagridView2)
            if (e.KeyCode == Keys.Escape)
            {
                dataGridView2.Visible = false;
                dataGridView1.CurrentCell = dataGridView1.Rows[howmanyrow].Cells[0];
                this.dataGridView1.BeginEdit(true);
            }
            #endregion

            #region 弹出第二个窗口,datagridView1显示一行数据
            if (e.KeyCode == Keys.Enter)
            {
                try
                {
                    ypmc = dataGridView2.CurrentRow.Cells[0].Value.ToString();
                    DataTable dt5 = new DataTable();
                    dt5.Clear();
                    //dataGridView1.Focus();
                    //if (dataGridView1.Focused)
                    //{
                    comStr = "select distinct t.YPMC,t.ypgg,t.YFDW,t2.cdqc,t1.lsjg,t1.ypph,t1.ypxq,t1.ypsl from yk_typk t,Yf_KCMX t1,YK_CDDZ t2 " +
                            "where t.YPMC='" + ypmc + "' and t1.YPXH=t.YPXH and  t1.ypcd=t2.ypcd and t1.yfsb='" + mainform.yfsb + "'";
                        dt5.Load(SqlHelper.ExecuteReader(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, comStr, null));

                        #region 第二种连接数据库查询方法
                        //if (mainform.yfsb == '3')
                        //{
                        //    comStr = "select distinct t.YPMC,t.yfgg,t.BFDW,t2.cdqc,t1.lsjg from yk_typk t,YF_KCMX t1,YK_CDDZ t2 where t.YPMC='" + ypmc + "' and t1.YPXH=t.YPXH and  t1.ypcd=t2.ypcd";
                        //    //dt4.Load(SqlHelper.ExecuteReader(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, comStr, null));
                        //    SqlConnection conn = new SqlConnection("Data Source=Localhost;database=yf;uid=sa;pwd=123;timeout=60;");
                        //    SqlDataAdapter da = new SqlDataAdapter(comStr, conn);
                        //    if (conn.State == ConnectionState.Closed)
                        //    {
                        //        conn.Open();
                        //    }
                        //    da.Fill(dt4);
                        //    conn.Close();
                        //}
                        //else
                        //{
                        //    comStr = "select distinct t.YPMC,t.ypgg,t.yFDW,t2.cdqc,t1.lsjg from yk_typk t,YF_KCMX t1,YK_CDDZ t2 where t.YPMC='" + ypmc + "' and t1.YPXH=t.YPXH and  t1.ypcd=t2.ypcd";
                        //    //dt4.Load(SqlHelper.ExecuteReader(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, comStr, null));
                        //    SqlConnection conn = new SqlConnection("Data Source=Localhost;database=yf;uid=sa;pwd=123;timeout=60;");
                        //    SqlDataAdapter da = new SqlDataAdapter(comStr, conn);
                        //    if (conn.State == ConnectionState.Closed)
                        //    {
                        //        conn.Open();
                        //    }
                        //    da.Fill(dt4);
                        //    conn.Close();
                        //}
                        #endregion
                    //}
                    if (dt5.Rows.Count>1)   //判断药品是否不止一个批次，>1表示不止一个批次
                    {
                        dataGridView3.Rows.Clear();
                        for (int i = 0; i < dt5.Rows.Count; i++)
                        {
                            dataGridView3.Rows.Add(1);
                            dataGridView3.Rows[i].Cells[0].Value = dt5.Rows[i][5].ToString();
                            dataGridView3.Rows[i].Cells[1].Value = dt5.Rows[i][3].ToString();
                            dataGridView3.Rows[i].Cells[2].Value = dt5.Rows[i][7].ToString();
                        }

                        dataGridView2.Visible = false;
                        dataGridView3.Visible = true;
                        dataGridView3.Focus();
                    }
                    else                  //药品只有一个批次
                    {
                        //不能重复录入出库药品
                        for (int i = 0; i < howmanyrow; i++)   
                        {
                            if (dt5.Rows[0][0].ToString() == dataGridView1.Rows[i].Cells[0].Value.ToString())
                            {
                                dataGridView2.Visible = false;
                                dataGridView1.Rows[howmanyrow].Cells[0].Value = null;
                                MessageBox.Show("该药品已存在，请修改此药药品量！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                                dataGridView1.CurrentCell = dataGridView1.Rows[howmanyrow].Cells[0];
                                this.dataGridView1.BeginEdit(true);
                                return;
	                        }                            
                        }
                        for (int i = 0; i < 5; i++)
                        {
                            dataGridView1.Rows[howmanyrow].Cells[i].Value = dt5.Rows[0][i].ToString();
                        }
                        dataGridView1.Rows[howmanyrow].Cells[7].Value = dt5.Rows[0][5].ToString();
                        dataGridView1.Rows[howmanyrow].Cells[8].Value = Convert.ToDateTime(dt5.Rows[0][6]).ToString("yyyy-MM-dd").Replace('-', '.');
                        dataGridView1.Rows[howmanyrow].Cells[9].Value = dt5.Rows[0][7].ToString();
                        dataGridView2.Visible = false;

                        //gl_ypckd_1 yprkd_1 = new gl_ypckd_1();
                        //yprkd_1.ShowDialog();
                        //if (gl_ypckd_1.yprkd_1 == 0)
                        //{
                        //    dataGridView1.Rows[howmanyrow].Cells[7].Value = gl_ypckd_1.ybph;
                        //    dataGridView1.Rows[howmanyrow].Cells[8].Value = Convert.ToDateTime(gl_ypckd_1.sxrq).ToString("yyyy-MM-dd").Replace('-', '.'); ;

                        dataGridView1.CurrentCell = dataGridView1.Rows[howmanyrow].Cells[5];
                        this.dataGridView1.BeginEdit(true);
                        //}
                        //else
                        //{
                        //    dataGridView1.Rows.RemoveAt(dataGridView1.Rows.Count - 1);
                        //    dataGridView1.Rows.Add(1);
                        //    dataGridView1.CurrentCell = dataGridView1.Rows[howmanyrow].Cells[0];
                        //}
                        //dataGridView2.Visible = false;
                    }
                }
                catch (Exception)
                {
                    MessageBox.Show("药品序号YPXH正在维护,暂不能使用", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    dataGridView2.Visible = false;
                    dataGridView1.CurrentCell = dataGridView1.Rows[howmanyrow].Cells[0];
                    this.dataGridView1.BeginEdit(true);
                }
            }
            #endregion
        }

        private void dataGridView3_KeyDown(object sender, KeyEventArgs e)
        {
            #region Esc关闭，不同批号窗口(DatagridView3)
            if (e.KeyCode == Keys.Escape)
            {
                dataGridView3.Visible = false;
                dataGridView1.CurrentCell = dataGridView1.Rows[howmanyrow].Cells[0];
                this.dataGridView1.BeginEdit(true);
            }
            #endregion

            #region 弹出第三个窗口,datagridView1显示一行数据
            if (e.KeyCode == Keys.Enter)
            {
                string ypph = dataGridView3.CurrentRow.Cells[0].Value.ToString();
                DataTable dt6 = new DataTable();
                dt6.Clear();
                //dataGridView1.Focus();
                //if (dataGridView1.Focused)
                //{
                comStr = "select distinct t.YPMC,t.ypgg,t.YFDW,t2.cdqc,t1.lsjg,t1.ypph,t1.ypxq,t1.ypsl from yk_typk t,Yf_KCMX t1,YK_CDDZ t2 " +
                        "where t.YPMC='" + ypmc + "' and t1.yfsb='" + mainform.yfsb + "' and t1.ypph='" + ypph + "' and t1.YPXH=t.YPXH and  t1.ypcd=t2.ypcd";
                dt6.Load(SqlHelper.ExecuteReader(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, comStr, null));

                //不能重复录入出库药品
                for (int i = 0; i < howmanyrow; i++)
                {
                    if (dt6.Rows[0][0].ToString() == dataGridView1.Rows[i].Cells[0].Value.ToString() &&
                        dt6.Rows[0][5].ToString() == dataGridView1.Rows[i].Cells[7].Value.ToString() && 
                        Convert.ToDateTime(dt6.Rows[0][6]).ToString("yyyy-MM-dd").Replace('-', '.') == dataGridView1.Rows[i].Cells[8].Value.ToString())
                    {
                        dataGridView3.Visible = false;
                        dataGridView1.Rows[howmanyrow].Cells[0].Value = null;
                        MessageBox.Show("该药品已存在，请修改此药药品量！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        dataGridView1.CurrentCell = dataGridView1.Rows[howmanyrow].Cells[0];
                        this.dataGridView1.BeginEdit(true);
                        return;
                    }
                }
                for (int i = 0; i < 5; i++)
                {
                    dataGridView1.Rows[howmanyrow].Cells[i].Value = dt6.Rows[0][i].ToString();
                }
                dataGridView1.Rows[howmanyrow].Cells[7].Value = dt6.Rows[0][5].ToString();
                dataGridView1.Rows[howmanyrow].Cells[8].Value = Convert.ToDateTime(dt6.Rows[0][6]).ToString("yyyy-MM-dd").Replace('-', '.');
                dataGridView1.Rows[howmanyrow].Cells[9].Value = dt6.Rows[0][7].ToString(); 
                dataGridView3.Visible = false;

                dataGridView1.CurrentCell = dataGridView1.Rows[howmanyrow].Cells[5];
                this.dataGridView1.BeginEdit(true);
            }
            #endregion
        }

        #region 让单元格处于编辑状态
        private void dataGridView1_CellEnter(object sender, DataGridViewCellEventArgs e)
        {
            #region 出库数量的列总在编辑状态
            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {
                if (dataGridView1.CurrentCell == dataGridView1.Rows[i].Cells[5] && dataGridView1.Rows[i].Cells[0].Value != null)
                {
                    dataGridView1.Rows[i].Cells[5].ReadOnly = false;
                    this.dataGridView1.BeginEdit(true);
                }
                if (dataGridView1.CurrentCell == dataGridView1.Rows[i].Cells[5] && dataGridView1.Rows[i].Cells[0].Value == null)
                {
                    dataGridView1.Rows[i].Cells[5].ReadOnly = true;  //datagridView最后一行第一列为空时，第六列不能编辑
                }
            }
            #endregion

            #region 药品名称的列，设置成编辑状态（选择性）
            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {
                if (dataGridView1.Rows[i].Cells[1].Value != null)
                {
                    dataGridView1.Rows[i].Cells[0].ReadOnly = true;
                }
            }
            if (dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells[0].Value == null)
            {
                this.dataGridView1.BeginEdit(true);
            }
            #endregion
        }
        #endregion

        #region 删除按钮
        private void button2_Click(object sender, EventArgs e)
        {
            if (dataGridView1.Rows.Count != 0)
            {
                dataGridView1.Rows.RemoveAt(dataGridView1.CurrentRow.Index);
                if (dataGridView1.Rows.Count == 0)
                {
                    dataGridView1.Rows.Add(1);
                }
            }
        }
        #endregion

        #region 退出按钮
        private void button4_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion

        /// <summary>
        /// 确认按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            if (dataGridView1.Rows.Count > 1)
            {
                comStr = "select ckfs from YF_cKFS t where t.fsmc='" + textBox1.Text.ToString() + "' and t.yfsb='" + mainform.yfsb + "'";
                string ckfs = (SqlHelper.ExecuteScalar(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, comStr, null)).ToString();
                //DataTable dt=new DataTable ();
                //dt.Load(SqlHelper.ExecuteReader(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, comStr, null));
                //string rkfs = dt.Rows[0][0].ToString();
                //插入出库表
                string comStr1 = "insert into yf_ck01(yfsb,ckfs,ckdh,ckrq,ckbz,ckpb,czgh) values " +
                    "('" + mainform.yfsb + "','" + ckfs + "','" + label6.Text.ToString() + "'," +
                    "'" + DateTime.Now.ToLocalTime().ToString() + "','" + textBox3.Text.ToString() + "','0','" + Program.ygbh + "')";
                SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, comStr1, null);       //出库表增加记录

                for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
                {
                    string aa = dataGridView1.Rows[i].Cells[0].Value.ToString();
                    string bb = dataGridView1.Rows[i].Cells[1].Value.ToString();
                    string cc = dataGridView1.Rows[i].Cells[2].Value.ToString();
                    string dd = dataGridView1.Rows[i].Cells[3].Value.ToString();
                    string ee = dataGridView1.Rows[i].Cells[4].Value.ToString();
                    string ff = dataGridView1.Rows[i].Cells[5].Value.ToString();
                    string gg = dataGridView1.Rows[i].Cells[6].Value.ToString();
                    string hh = dataGridView1.Rows[i].Cells[7].Value.ToString();
                    string ii = dataGridView1.Rows[i].Cells[8].Value.ToString();

                    string comStr2 = "select t.ypxh from YK_TYPK t where t.ypmc='" + aa + "' and t.ypgg='" + bb + "' and t.yfdw='" + cc + "'";
                    string ypxh = (SqlHelper.ExecuteScalar(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, comStr2, null)).ToString();
                    string comStr3 = "select t.ypcd from YK_CDDZ t where t.cdqc='" + dd + "'";
                    string ypcd = (SqlHelper.ExecuteScalar(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, comStr3, null)).ToString();
                    //插入出库明细表
                    string comStr4 = "insert into YF_cK02(yfsb,ckfs,ckdh,ypxh,ypcd,ypgg,yfdw,lsjg,cksl,lsje,ypph,ypxq) values " +
                        "('" + mainform.yfsb + "','" + ckfs + "','" + label6.Text.ToString() + "','" + ypxh + "','" + ypcd + "','" + bb + "'," +
                        "'" + cc + "','" + ee + "','" + ff + "','" + gg + "','" + hh + "','" + ii + "')";
                    SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, comStr4, null);      //出库明细表增加记录
                }
                tan_tishi = 1;
                MessageBox.Show("出库单保存成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.Close();
            }
            else
            {
                MessageBox.Show("出库单为空，不能确认", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        #region datagridView只能输出数字 winform下c#实现datagridview单元格只能输出数字,"02"还不能屏蔽输入
        // 自定义事件   
        private void Cells_KeyPress(object sender, KeyPressEventArgs e)
        {
            ////键盘"0"和退格键可以按
            //if (e.KeyChar != '\b' && e.KeyChar != '0')
            //{
            //    for (int i = 0; i < dataGridView1.Rows.Count; i++)
            //    {
            //        if (dataGridView1.CurrentCell == dataGridView1.Rows[i].Cells[5])
            //        {
            //            e.Handled = true;
            //        }
            //    }
            //}
            ////键盘"1"到"9"可以按
            //if (e.KeyChar >= '1' && e.KeyChar <= '9')
            //{
            //    for (int i = 0; i < dataGridView1.Rows.Count; i++)
            //    {
            //        if (dataGridView1.CurrentCell == dataGridView1.Rows[i].Cells[5])
            //        {
            //            if (dataGridView1.Rows[i].Cells[5].Value!=null)
            //            {
            //                if (!(dataGridView1.Rows[i].Cells[5].Value.ToString().Length == 1 && dataGridView1.Rows[i].Cells[5].Value.ToString() == "0"))
            //                {
            //                    e.Handled = false;
            //                }
            //            }
            //        }
            //    }
            //}
            if ((e.KeyChar < '0' || e.KeyChar > '9') && e.KeyChar != '\b')
            {
                for (int i = 0; i < dataGridView1.Rows.Count; i++)
                {
                    if (dataGridView1.CurrentCell == dataGridView1.Rows[i].Cells[5])
                    {
                        e.Handled = true;
                    }
                }
            }
        }
        public DataGridViewTextBoxEditingControl CellEdit = null; // 声明 一个 CellEdit   
        private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {

            CellEdit = (DataGridViewTextBoxEditingControl)e.Control;
            CellEdit.SelectAll();
            CellEdit.KeyPress += Cells_KeyPress;
        }
        #endregion

        //未保存就退出窗体，弹出提示框
        private void gl_ypckd_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (tan_tishi == 1)
            {
                return;
            }
            if (dataGridView1.Rows.Count == 0)
            {
                return;
            }
            DialogResult result = MessageBox.Show("该出库单是否保存？", "提示", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);

            if (result == DialogResult.Yes)
            {
                if (dataGridView1.Rows.Count > 1)
                {
                    comStr = "select ckfs from YF_cKFS t where t.fsmc='" + textBox1.Text.ToString() + "' and t.yfsb='" + mainform.yfsb + "'";
                    string ckfs = (SqlHelper.ExecuteScalar(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, comStr, null)).ToString();
                    //DataTable dt=new DataTable ();
                    //dt.Load(SqlHelper.ExecuteReader(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, comStr, null));
                    //string rkfs = dt.Rows[0][0].ToString();
                    string comStr1 = "insert into yf_ck01(yfsb,ckfs,ckdh,ckrq,ckbz,ckpb,czgh) values " +
                        "('" + mainform.yfsb + "','" + ckfs + "','" + label6.Text.ToString() + "'," +
                        "'" + DateTime.Now.ToLocalTime().ToString() + "','" + textBox3.Text.ToString() + "','0','" + Program.ygbh + "')";
                    SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, comStr1, null);       //出库表增加记录

                    for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
                    {
                        string aa = dataGridView1.Rows[i].Cells[0].Value.ToString();
                        string bb = dataGridView1.Rows[i].Cells[1].Value.ToString();
                        string cc = dataGridView1.Rows[i].Cells[2].Value.ToString();
                        string dd = dataGridView1.Rows[i].Cells[3].Value.ToString();
                        string ee = dataGridView1.Rows[i].Cells[4].Value.ToString();
                        string ff = dataGridView1.Rows[i].Cells[5].Value.ToString();
                        string gg = dataGridView1.Rows[i].Cells[6].Value.ToString();
                        string hh = dataGridView1.Rows[i].Cells[7].Value.ToString();
                        string ii = dataGridView1.Rows[i].Cells[8].Value.ToString();

                        string comStr2 = "select t.ypxh from YK_TYPK t where t.ypmc='" + aa + "' and t.ypgg='" + bb + "' and t.yfdw='" + cc + "'";
                        string ypxh = (SqlHelper.ExecuteScalar(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, comStr2, null)).ToString();
                        string comStr3 = "select t.ypcd from YK_CDDZ t where t.cdqc='" + dd + "'";
                        string ypcd = (SqlHelper.ExecuteScalar(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, comStr3, null)).ToString();

                        string comStr4 = "insert into YF_cK02(yfsb,ckfs,ckdh,ypxh,ypcd,ypgg,yfdw,lsjg,cksl,lsje,ypph,ypxq) values " +
                            "('" + mainform.yfsb + "','" + ckfs + "','" + label6.Text.ToString() + "','" + ypxh + "','" + ypcd + "','" + bb + "'," +
                            "'" + cc + "','" + ee + "','" + ff + "','" + gg + "','" + hh + "','" + ii + "')";
                        SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, comStr4, null);      //出库明细表增加记录
                    }
                    MessageBox.Show("出库单保存成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    this.Close();
                }
                else
                {
                    MessageBox.Show("出库单为空，请重新键出！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    e.Cancel = true;  //不关闭窗口  
                }
            }

            if (result == DialogResult.Cancel)
            {
                e.Cancel = true;  //不关闭窗口  
            }
        }
    }
}
